package net.arogarth.aws.android.manager.dbmodels;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import net.arogarth.aws.android.manager.DatabaseAdapter;

public class AwsAccount extends DbObject {
	private Long id = null;
	private String name = "";
	private String defaultRegion = "";
	private String accessKey = "";
	private String secretKey = "";
	
	public AwsAccount() {}
	
	public AwsAccount(Long id, String name, String accessKey, String secretKey) {
		this.id = id;
		this.name = name;
		this.accessKey = accessKey;
		this.secretKey = secretKey;
	}
	
	public AwsAccount(String name, String accessKey, String secretKey) {
		this.id = null;
		this.name = name;
		this.accessKey = accessKey;
		this.secretKey = secretKey;
	}
	
	public Long getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public String getAccessKey() {
		return accessKey;
	}
	public String getSecretKey() {
		return secretKey;
	}

	public void setDefaultRegion(String defaultRegion) {
		this.defaultRegion = defaultRegion;
	}
	
	public synchronized String getDefaultRegion() {
		return defaultRegion;
	}

	public synchronized void setName(String name) {
		this.name = name;
	}

	public synchronized void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}

	public synchronized void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}

	/////////////////////////////////////////////////////////////
	/// Database Methods
	/////////////////////////////////////////////////////////////
	public AwsAccount load(Long id) {
		@SuppressWarnings("unchecked")
		List<AwsAccount> list = (List<AwsAccount>) this.list(String.format("id = %s", id));
		if(list.size() > 0) return list.get(0);
		
		return null;
	}

	public List<?> list(String where) {
		String qry = "SELECT * FROM " + DatabaseAdapter.APP_DB_TABLE_ACCOUNT_NAME;
		if(where != "")
			qry += " WHERE " + where;
		
		DatabaseAdapter dba = new DatabaseAdapter().open();
		Cursor c = dba.getDatabase().rawQuery(qry, new String[] {});
		
		List<AwsAccount> accounts = new ArrayList<AwsAccount>();

		while(c.moveToNext()) {
			Long id = c.getLong(0);
			String name = c.getString(1);
			String default_zone = c.getString(2);
			String accessKey = c.getString(3);
			String secretKey = c.getString(4);
			
			AwsAccount account = new AwsAccount(id, name, accessKey, secretKey); 
			account.setDefaultRegion(default_zone);
			accounts.add(account);
		}
		
		dba.close();
		return accounts;
	}

	public void save() {
		DatabaseAdapter dba = new DatabaseAdapter().open();
		if(this.id == null) { // Save
			
			ContentValues values = new ContentValues();
			values.put("name", this.name);
			values.put("default_region", this.defaultRegion);
			values.put("accesskey", this.accessKey);
			values.put("secretkey", this.secretKey);
			this.id = dba.getDatabase().insert(DatabaseAdapter.APP_DB_TABLE_ACCOUNT_NAME, "0", values);
			
		} else { // Update
			
			String qry = "UPDATE " + DatabaseAdapter.APP_DB_TABLE_ACCOUNT_NAME;
			qry += " SET ";
			qry += String.format("name = '%s',", this.name);
			qry += String.format("default_region = '%s',", this.defaultRegion);
			qry += String.format("accesskey = '%s',", this.accessKey);
			qry += String.format("secretkey = '%s'", this.secretKey);
			qry += " WHERE id = " + this.id;
			dba.getDatabase().execSQL(qry);
			
		}
		
		dba.close();
	}

	public void delete() {
		DatabaseAdapter dba = new DatabaseAdapter().open();
		dba.getDatabase().delete(DatabaseAdapter.APP_DB_TABLE_ACCOUNT_NAME, "id = " + this.id.toString(), new String[] { });
		this.id = null;
		dba.close();
	}
}
